/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ControlLayer;

import DBLayer.DBOrder;
import DBLayer.DBSupplier;
import ModelLayer.Actor;
import ModelLayer.Employee;
import ModelLayer.Order;
import ModelLayer.Product;
import ModelLayer.Role;
import ModelLayer.Supplier;
import java.io.InvalidObjectException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Evil
 */
public class OrderController
{
    private DBSupplier dbSupplier;
    private Order cartOrder;

    public OrderController()
    {
        dbSupplier = new DBSupplier();
    }

    public Order getCartOrder()
    {
        return cartOrder;
    }

    public void createCartOrder(int supplierID)
    {
        try
        {
            Actor actor = GlobalInstances.getInstance().getCurrentEmployee();
            Employee employee = (Employee) actor.getRole(Role.Employee);
            Supplier supplier = dbSupplier.getObject(supplierID);
            if (supplier != null)
            {
                cartOrder = new Order(employee, supplier);
                cartOrder.setDeliveryStatus("Not delivered");
            }
        }
        catch (InvalidObjectException ex)
        {
            Logger.getLogger(OrderController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addProduct(Product product, int quantity)
    {
        getCartOrder().addProduct(product, quantity);
    }

   public void removeProduct(int index)
    {
       getCartOrder().removeProduct(index);
       
   }

    public void completeOrder()
    {
        DBOrder dbOrder = new DBOrder();
        dbOrder.SaveObject(cartOrder);
        sendOrder(cartOrder);
    }

    public void sendOrder(Order order)
    {
        //advanced code to send stuff to who ever is the supplier
    }
}
